// pages/calculation/calculation.js

const getDateDiff = require('../../utils/time.js').getDateDiff
const formatTime = require('../../utils/time.js').formatTimes
const getNewDate = require('../../utils/time.js').getNewDate

const util = require('../../utils/util.js')
Page({
  data: {
    time: 0,
    days: "",
    color: 'cyan',
    TabCur: 0,
    // flag: , 
    tabList: ["日期间隔", "日期推算"],
    picker: ['往后推', '往前推'], //0 往后推 1 往前推
    index: 0,
    value1: 0,

    changeDays: ""

  },

  tabSelect(e) {
    console.log(e);
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },

  PickerChange(e) {
    // console.log("推算方式：")
    // console.log(e.detail.value);
    this.setData({
      index: parseInt(e.detail.value)
    })
  },


  /**
生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  //获取输入的天数
  daysChange(e) {
    this.setData({
      changeDays: e.detail.value
    })
  },

  bindBeginDate: function(e) {
    console.log(e.detail.value)
    console.log(e.detail.value.replace(/-/g, '/'))
    let a = e.detail.value.replace(/-/g, '/');
    this.setData({
      date: e.detail.value,
      time: (new Date(a)).toString(),
    })
  },

  bindEndDate: function(e) {
    console.log(e.detail.value)
    console.log(e.detail.value.replace(/-/g, '/'))
    let a = e.detail.value.replace(/-/g, '/');
    this.setData({
      dateEnd: e.detail.value,
      time: (new Date(a)).toString(),
    })
  },

  calculate: function() {

    const daysExample = getDateDiff(this.data.date, this.data.dateEnd)
    console.log("日期间隔: " + daysExample)


    const that = this;
    let startTime = new Date(this.data.date.replace(/-/g, '/')).getTime();
    let endTime = new Date(this.data.dateEnd.replace(/-/g, '/')).getTime();
    let days = (endTime - startTime) / 1000 / 60 / 60 / 24;
    if (days < 0) {
      wx.showToast({
        title: '计算失败',
        icon: 'none',
        duration: 2000,
      })
    } else {
      that.setData({
        days: "相差" + days + "天",
      })
      wx.showToast({
        title: '计算成功',
        icon: 'success',
        duration: 2000,
      })
    }
  },


  calculate1: function() {

    let newDate = 0
    console.log(this.data.index)
    console.log(this.data.changeDays)
    if (this.data.index === 0) {
      newDate = getNewDate(this.data.date, parseInt(this.data.changeDays))
    } else {
      newDate = getNewDate(this.data.date, -parseInt(this.data.changeDays))
    }
    console.log(newDate)
    this.setData({
      newDate: "推算结果：" + newDate
    })

    // // if (flag){
    // const newDate = getNewDate(this.data.date, parseInt(this.data.changeDays))
    // this.setData({
    //   newDate: newDate
    // })
    // // }else{
    // const beforeDate = getBeforerDate(this.data.date, parseInt(this.data.changeDays))
    // this.setData({
    //   beforeDate: beforeDate
    // })
    // }

    //接下来只是示例
    //我看到你用date变量保存推算的日期， 然后我定义了changeDays变量获取输入的天数(由于获取的天数是字符串形式，要用parseInt()转换成数字类型)
    // let newDate = getNewDate(this.data.date, parseInt(this.data.changeDays))
    // console.log("推算后的日期：" + newDate)

  },



  /**
生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})